package com.yx.elastic.elasticcm.controller.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yx.elastic.elasticcm.base.ISearchException;
import com.yx.elastic.elasticcm.config.ResTData;
import com.yx.elastic.elasticcm.entity.ElasAnalyzerCharacter;
import com.yx.elastic.elasticcm.service.AnalyzerCharacterService;
import com.yx.elastic.elasticcm.util.StringUtils;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by chenzp on 18-7-27.
 */
@RestController
@RequestMapping("/analyzer/character")
public class AnalyzerCharacterController {

    @Autowired
    private AnalyzerCharacterService analyzerCharacterService;



    /**
     * 字符过滤器列表
     * @param modelAndView
     * @return
     */
    @RequestMapping("/characterView")
    public ModelAndView characterView(ModelAndView modelAndView) {
        modelAndView.setViewName("/analyzer/character");
        return modelAndView;
    }

    /**
     * 添加字符过滤器
     * @param elasAnalyzerCharacter
     * @return
     */
    @PostMapping("/add")
    public ResTData addCaracter(ElasAnalyzerCharacter elasAnalyzerCharacter) {
        try {
            analyzerCharacterService.index(elasAnalyzerCharacter);
            return new ResTData();
        } catch (ISearchException e) {
            e.printStackTrace();
            return new ResTData(e.getMessage());
        }
    }


    /**
     * 列表数据
     * @param
     * @return
     */
    @RequestMapping("/list")
    public ResTData list(int page,int limit,String characterType,String name) {
        try {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.from(page);
            searchSourceBuilder.size(limit);

            if (StringUtils.isNotNullAndEmpty(characterType)) {
                TermQueryBuilder termQueryBuilder = new TermQueryBuilder("characterType",characterType);
                searchSourceBuilder.query(termQueryBuilder);
            }

            if (StringUtils.isNotNullAndEmpty(name)) {
                WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("name.keyword","*"+name+"*");
                searchSourceBuilder.query(wildcardQueryBuilder);
            }

            SearchResponse query = analyzerCharacterService.query(searchSourceBuilder);
            return new ResTData(query);
        } catch (ISearchException e) {
            e.printStackTrace();
            return new ResTData(e.getMessage());
        }
    }

    /**
     * 根据ID删除文档
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResTData deleteId(@PathVariable("id") String id) {
        try {
            DeleteResponse deleteResponse = analyzerCharacterService.deleteById(id);
            return new ResTData();
        } catch (ISearchException e) {
            e.printStackTrace();
            return new ResTData(e.getMessage());
        }
    }

}
